const Koa = require('koa')
const path = require('path')
const bodyparser = require('koa-bodyparser')
const favicon = require('koa-favicon')
const session = require('koa-session')
// const KoaStatic = require('koa-static')
// const mount = require('koa-mount')
// const config = require('./config')
const loggerMiddleware = require('./middleware/logger.middleware')
const commonMiddleware = require('./middleware/common.middleware')
// const historyFallback = require('./middleware/history-fallback.middleware')
const router = require('./router')

const app = new Koa()

app.keys = ['UU1acylnNQgu0f8hGLoZNw==']
app.use(bodyparser())

app.use(favicon(path.resolve(__dirname, './favicon.ico')))

app.use(session({
    key: 'sso.sid',
    maxAge: 'session', // 'session' 关闭浏览器失效
    httpOnly: true
    // store: null // 注意: koa-session 是存储在cookie里的。
}, app))

app.use(loggerMiddleware)
app.use(commonMiddleware)
app.use(router.routes()).use(router.allowedMethods())

// for web history api
// app.use(historyFallback())
// app.use(KoaStatic(config.webDistDir))

module.exports = app
